{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Die assembly\n",
    "\n",
    "With gdsfactory you can easily go from a simple component, to a component with many components inside.\n",
    "\n",
    "In the same way that you need to Layout for DRC (Design Rule Check) clean devices, you have to layout obeying the Design for Test (DFT) and Design for Packaging rules.\n",
    "\n",
    "## Design for Test\n",
    "\n",
    "To measure your chips after fabrication you need to decide your test configurations. This includes Design For Testing Rules like:\n",
    "\n",
    "- `Individual input and output fibers` versus `fiber array`. You can use `add_fiber_array` for easier testing and higher throughput, or `add_fiber_single` for the flexibility of single fibers.\n",
    "- Fiber array pitch (127um or 250um) if using a fiber array.\n",
    "- Pad pitch for DC and RF high speed probes (100, 125, 150, 200um). Probe configuration (GSG, GS ...)\n",
    "- Test layout for DC, RF and optical fibers.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import partial\n",
    "\n",
    "import gdsfactory as gf\n",
    "\n",
    "# After you run gf.config.rich_output(), gdsfactory will automatically generate and display a plot of the component's geometry instead of the text.\n",
    "gf.config.rich_output()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Pack\n",
    "\n",
    "Let us start with a resistance sweep, where you change the resistance width to measure sheet resistance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_resistance_sweep_info(c):\n",
    "    # A helper function is defined to add specific metadata to a component's .info attribute.\n",
    "    # This information, like \"doe\": \"resistance_sweep\" (Design of Experiments) and \"measurement\": \"iv\" (current-voltage),\n",
    "    # is used by automated testing and data analysis tools.\n",
    "    c.info[\"doe\"] = \"resistance_sweep\"\n",
    "    c.info[\"analysis\"] = \"[iv_resistance]\"\n",
    "    c.info[\"analysis_parameters\"] = \"[{}]\"\n",
    "    c.info[\"ports_electrical\"] = 2\n",
    "    c.info[\"ports_optical\"] = 0\n",
    "    c.info[\"measurement\"] = \"iv\"\n",
    "    c.info[\"measurement_parameters\"] = \"{}\"\n",
    "    return c\n",
    "\n",
    "# A list of three resistance_sheet components is created, each with a different width (1, 10, and 50 µm).\n",
    "sweep = [gf.components.resistance_sheet(width=width) for width in [1, 10, 50]]\n",
    "\n",
    "# The add_resistance_sweep_info function is applied to each of the three resistor components, embedding the testing metadata into each one.\n",
    "sweep_with_info = [add_resistance_sweep_info(c) for c in sweep]\n",
    "\n",
    "# The gf.pack function takes the list of three components and automatically arranges them in a compact way,\n",
    "# creating a single, larger component that contains them. m[0] retrieves this final packed component.\n",
    "m = gf.pack(sweep_with_info)\n",
    "c = m[0]\n",
    "c.draw_ports()\n",
    "c.pprint_ports()\n",
    "c.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sweep_with_info[0].info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "Then we add spirals with different lengths to measure the waveguide propagation loss. You can use both a fiber array or single fiber."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "@gf.cell\n",
    "\n",
    "# This line creates the basic spiral waveguide. The **kwargs allows you to pass any parameters (like length, width, etc.) to the underlying spiral component.\n",
    "def spiral_gc(**kwargs):\n",
    "    \"\"\"Returns spiral with Grating Couplers.\"\"\"\n",
    "    c = gf.components.spiral(**kwargs)\n",
    "    c = gf.routing.add_fiber_array(c)\n",
    "    c.info[\"doe\"] = \"spirals_sc\"  # Strip Cband spirals\n",
    "    c.info[\"measurement\"] = \"optical_spectrum\"\n",
    "    c.info[\"measurement_parameters\"] = \"{}\"\n",
    "    c.info[\"analysis\"] = \"[power_envelope]\"\n",
    "    c.info[\"analysis_parameters\"] = \"[]\"\n",
    "    c.info[\"ports_optical\"] = 4\n",
    "    c.info[\"ports_electrical\"] = 0\n",
    "    c.info.update(kwargs)\n",
    "    return c\n",
    "\n",
    "\n",
    "c = spiral_gc(length=100)\n",
    "c.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "c.info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "sweep = [spiral_gc(length=length) for length in [100, 200, 300]]\n",
    "m = gf.pack(sweep)\n",
    "c = m[0]\n",
    "c.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "You can also add some physical labels that will be fabricated.\n",
    "For example you can add prefix `S` at the `north-center` of each spiral using `text_rectangular` which is DRC clean and anchored on `nc` (north-center)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_metal = partial(gf.components.text_rectangular_multi_layer, layers=(\"M1\",))\n",
    "\n",
    "m = gf.pack(sweep, text=text_metal, text_anchors=(\"cw\",), text_prefix=\"s\")\n",
    "c = m[0]\n",
    "c.show()\n",
    "c.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "## Grid\n",
    "\n",
    "You can also pack components with a constant spacing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = gf.grid(sweep)\n",
    "g.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "gh = gf.grid(sweep, shape=(1, len(sweep)))\n",
    "gh.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "gh_ymin = gf.grid(sweep, shape=(len(sweep), 1), align_x=\"xmin\")\n",
    "gh_ymin.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "Additionally, it allows you to add text labels to each element of the sweep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "gh_ymin = gf.grid_with_text(\n",
    "    \n",
    "    # shape=(len(sweep), 1): This defines the grid as having a single column and a number of rows equal to the number of components in the sweep list.\n",
    "    # align_x=\"xmax\": This aligns all the components in the column so that their right edges (xmax) are in a straight vertical line.\n",
    "    # text=text_metal: This provides a list of text strings that will be placed as labels next to each corresponding component in the grid.\n",
    "    sweep, shape=(len(sweep), 1), align_x=\"xmax\", text=text_metal\n",
    ")\n",
    "gh_ymin.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "gh_ymin = gf.grid_with_text(\n",
    "    sweep,\n",
    "    shape=(len(sweep), 1),\n",
    "    align_x=\"xmax\",\n",
    "    text=text_metal,\n",
    "    labels=(\"S100\", \"S200\", \"S300\"),\n",
    ")\n",
    "gh_ymin.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "You have 2 ways of defining a mask:\n",
    "\n",
    "1. in YAML\n",
    "2. in Python\n",
    "\n",
    "## YAML Component\n",
    "\n",
    "You can also define your Component in the YAML format thanks to `gdsfactory.read.from_yaml`\n",
    "\n",
    "You need to define:\n",
    "\n",
    "- Instances\n",
    "- Placements\n",
    "- Routes (optional)\n",
    "\n",
    "And you can leverage:\n",
    "\n",
    "1. `pack_doe`\n",
    "2. `pack_doe_grid`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "`pack_doe` places components as compact as possible."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20",
   "metadata": {},
   "source": [
    "`pack_doe_grid` places each component on a regular grid."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "\n",
    "c = gf.read.from_yaml(\n",
    "    \"\"\"\n",
    "name: mask_compact\n",
    "\n",
    "instances:\n",
    "  rings:\n",
    "\n",
    "  #  This is a special function that creates a Design of Experiments array.\n",
    "    component: pack_doe\n",
    "    settings:\n",
    "      \n",
    "      # settings: radius: [30, 50, 20, 40], length_x: [1, 2, 3]: It will create ring resonators with these different radii and coupling lengths.\n",
    "      doe: ring_single\n",
    "      settings:\n",
    "        radius: [30, 50, 20, 40]\n",
    "        length_x: [1, 2, 3]\n",
    "\n",
    "      # This tells the function to generate all possible combinations of the specified radius and length_x values.\n",
    "      do_permutations: True\n",
    "      function:\n",
    "\n",
    "        # After each unique ring is created, the add_fiber_array function is applied to it, adding grating couplers for testing.\n",
    "        function: add_fiber_array\n",
    "        settings:\n",
    "            fanout_length: 200\n",
    "\n",
    "\n",
    "  mzis:\n",
    "    component: pack_doe_grid\n",
    "    settings:\n",
    "      doe: mzi\n",
    "      settings:\n",
    "        delta_length: [10, 100]\n",
    "      do_permutations: True\n",
    "      spacing: [10, 10]\n",
    "      function: add_fiber_array\n",
    "\n",
    "placements:\n",
    "  rings:\n",
    "    xmin: 50\n",
    "\n",
    "  mzis:\n",
    "    xmin: rings,east\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "c.show()\n",
    "c.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "## Automated testing and analysis \n",
    "\n",
    "This is useful when you have a lot of components and you want to automate the testing process.\n",
    "\n",
    "There are two main ways to define which components are testable:\n",
    "\n",
    "1. Include a `doe` (Design of Experiments) field in the component.info dictionary, as well as all relevant test and analysis information.\n",
    "2. Include a GDS label in all component test points. There are many ways to define test points, but the most common is to use a GDS label with the format `<elec/opt>-<number_of_ports>-<cell_name>`. This way you can easily extract all test points from the GDS file.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import gdsfactory as gf\n",
    "\n",
    "\n",
    "@gf.cell\n",
    "def mzm_gc(length_x=10, **kwargs) -> gf.Component:\n",
    "    \"\"\"Returns a MZI with Grating Couplers.\n",
    "\n",
    "    Args:\n",
    "        length_x: length of the MZI.\n",
    "        kwargs: additional settings.\n",
    "    \"\"\"\n",
    "    c = gf.components.mzi2x2_2x2_phase_shifter(\n",
    "        length_x=length_x, auto_rename_ports=False, **kwargs\n",
    "    )\n",
    "    c = gf.routing.add_pads_top(c, port_names=[\"top_l_e1\", \"top_r_e3\"])\n",
    "    c = gf.routing.add_fiber_array(c)\n",
    "    c.info[\"doe\"] = \"mzm\"\n",
    "    c.info[\"measurement\"] = \"optical_spectrum\"\n",
    "    c.info[\"analysis\"] = \"[fsr]\"\n",
    "    c.info[\"analysis_parameters\"] = \"[]\"\n",
    "    c.info[\"ports_electrical\"] = 2\n",
    "    c.info[\"ports_optical\"] = 6\n",
    "    c.info[\"length_x\"] = length_x\n",
    "    c.info.update(kwargs)\n",
    "    return c\n",
    "\n",
    "\n",
    "def sample_reticle(grid: bool = False) -> gf.Component:\n",
    "    \"\"\"Returns MZI with TE grating couplers.\"\"\"\n",
    "\n",
    "    mzis = [mzm_gc(length_x=lengths) for lengths in [100, 200, 300]]\n",
    "    spirals = [spiral_gc(length=length) for length in [0, 100, 200]]\n",
    "    rings = []\n",
    "    for length_x in [10, 20, 30]:\n",
    "        ring = gf.components.ring_single_heater(length_x=length_x)\n",
    "        c = gf.components.add_fiber_array_optical_south_electrical_north(\n",
    "            component=ring,\n",
    "            electrical_port_names=[\"l_e2\", \"r_e2\"],\n",
    "            grating_coupler=gf.components.grating_coupler_te, \n",
    "            pad=gf.components.pad,\n",
    "            cross_section_metal='metal3'\n",
    "        ).copy()\n",
    "        c.name = f\"ring_{length_x}\"\n",
    "        c.info[\"doe\"] = \"ring_length_x\"\n",
    "        c.info[\"measurement\"] = \"optical_spectrum\"\n",
    "        c.info[\"ports_electrical\"] = 2\n",
    "        c.info[\"ports_optical\"] = 4\n",
    "        c.info[\"analysis\"] = \"[fsr]\"\n",
    "        c.info[\"analysis_parameters\"] = \"[]\"\n",
    "        c.info[\"length_x\"] = length_x\n",
    "        rings.append(c)\n",
    "\n",
    "    copies = 3  # Number of copies of each component.\n",
    "    components = mzis * copies + rings * copies + spirals * copies\n",
    "    if grid:\n",
    "        return gf.grid(components)\n",
    "    mask = gf.pack(components)\n",
    "    if len(mask) > 1:\n",
    "        mask = gf.pack(mask)\n",
    "    return mask[0]\n",
    "\n",
    "\n",
    "c = sample_reticle()\n",
    "c.show()\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": [
    "gf.labels.write_test_manifest(c, csvpath=\"sample_reticle.csv\")\n",
    "df = pd.read_csv(\"sample_reticle.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "source": [
    "You can see a test manifest example [here](https://docs.google.com/spreadsheets/d/1845m-XZM8tZ1tNd8GIvAaq7ZE-iha00XNWa0XrEOabc/edit#gid=233591479)\n",
    "\n",
    "## Automated testing with labels\n",
    "\n",
    "The GDS info is stored in the GDS file metadata and can be lost if the GDS file is modified with other tools that are not aware of the metadata. To avoid this, GDSFactory also supports a more traditional way of defining test points, using GDS labels. \n",
    "\n",
    "For example, lets say you want to label the rightmost port of a component with a GDS label `port_type-number_of_ports-cell_name`. You can do this with the following code:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gdsfactory as gf\n",
    "from gdsfactory.typings import LayerSpec\n",
    "\n",
    "layer_label = \"TEXT\"\n",
    "\n",
    "\n",
    "def label_farthest_right_port(\n",
    "    component: gf.Component, ports: gf.Port | list[gf.Port], layer: LayerSpec, text: str\n",
    ") -> gf.Component:\n",
    "    \"\"\"Adds a label to the right of the farthest right port in a given component.\n",
    "\n",
    "    Args:\n",
    "        component: The component to which the label is added.\n",
    "        ports: A list of ports to evaluate for positioning the label.\n",
    "        layer: The layer on which the label will be added.\n",
    "        text: The text to display in the label.\n",
    "    \"\"\"\n",
    "    rightmost_port = max(ports, key=lambda port: port.dx)\n",
    "\n",
    "    component.add_label(\n",
    "        text=text,\n",
    "        position=rightmost_port.dcenter,\n",
    "        layer=layer,\n",
    "    )\n",
    "    return component\n",
    "\n",
    "\n",
    "c = gf.Component()\n",
    "ref = c << gf.routing.add_pads_top(gf.components.wire_straight())\n",
    "\n",
    "# ref.ports: The function takes the list of all ports from the component reference ref.\n",
    "# It then iterates through these ports to find the one with the largest x-coordinate (the one that is farthest to the right).\n",
    "# c.add_label(...): Once the rightmost port is identified, the function adds a text label to the main component c at that port's position.\n",
    "label_farthest_right_port(c, ref.ports, layer=layer_label, text=\"elec-2-wire_straight\")\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "def spiral_gc(length: float = 0, **kwargs) -> gf.Component:\n",
    "    \"\"\"Returns a spiral double with Grating Couplers.\n",
    "\n",
    "    Args:\n",
    "        length: length of the spiral straight section.\n",
    "        kwargs: additional settings.\n",
    "\n",
    "    Keyword Args:\n",
    "        bend: bend component.\n",
    "        straight: straight component.\n",
    "        cross_section: cross_section component.\n",
    "        spacing: spacing between the spiral loops.\n",
    "        n_loops: number of loops.\n",
    "    \"\"\"\n",
    "    c0 = gf.c.spiral(length=length, **kwargs)\n",
    "    c = gf.routing.add_fiber_array(c0)\n",
    "    c.info[\"doe\"] = \"spirals_sc\"\n",
    "    c.info[\"measurement\"] = \"optical_spectrum\"\n",
    "    c.info[\"analysis\"] = \"[power_envelope]\"\n",
    "    c.info[\"analysis_parameters\"] = \"[]\"\n",
    "    c.info[\"ports_optical\"] = 4\n",
    "    c.info[\"ports_electrical\"] = 0\n",
    "    c.info.update(kwargs)\n",
    "\n",
    "    c.name = f\"spiral_gc_{length}\"\n",
    "    label_farthest_right_port(c, c.ports, layer=layer_label, text=f\"opt-4-{c.name}\")\n",
    "    return c\n",
    "\n",
    "\n",
    "c = spiral_gc(length=0)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mzi_gc(length_x=10, **kwargs) -> gf.Component:\n",
    "    \"\"\"Returns a MZI with Grating Couplers.\n",
    "\n",
    "    Args:\n",
    "        length_x: length of the MZI.\n",
    "        kwargs: additional settings.\n",
    "    \"\"\"\n",
    "    c = gf.components.mzi2x2_2x2_phase_shifter(\n",
    "        length_x=length_x, auto_rename_ports=False, **kwargs\n",
    "    )\n",
    "    c = gf.routing.add_pads_top(c, port_names=(\"top_l_e1\", \"top_r_e3\"))\n",
    "    c.name = f\"mzi_{length_x}\"\n",
    "    c = gf.routing.add_fiber_array(c)\n",
    "\n",
    "    c.info[\"doe\"] = \"mzi\"\n",
    "    c.info[\"measurement\"] = \"optical_spectrum\"\n",
    "    c.info[\"analysis\"] = \"[fsr]\"\n",
    "    c.info[\"analysis_parameters\"] = \"[]\"\n",
    "    c.info[\"ports_electrical\"] = 2\n",
    "    c.info[\"ports_optical\"] = 6\n",
    "    c.info[\"length_x\"] = length_x\n",
    "    c.info.update(kwargs)\n",
    "\n",
    "    c.name = f\"mzi_gc_{length_x}\"\n",
    "    label_farthest_right_port(\n",
    "        c,\n",
    "        c.ports.filter(port_type=\"vertical_te\"),\n",
    "        layer=layer_label,\n",
    "        text=f\"opt-{c.info['ports_optical']}-{c.name}\",\n",
    "    )\n",
    "    label_farthest_right_port(\n",
    "        c,\n",
    "        c.ports.filter(port_type=\"electrical\"),\n",
    "        layer=layer_label,\n",
    "        text=f\"elec-{c.info['ports_electrical']}-{c.name}\",\n",
    "    )\n",
    "    return c\n",
    "\n",
    "\n",
    "c = mzi_gc(length_x=10)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample_reticle_with_labels(grid: bool = False) -> gf.Component:\n",
    "    \"\"\"Returns MZI with TE grating couplers.\"\"\"\n",
    "\n",
    "    mzis = [mzi_gc(length_x=lengths) for lengths in [100, 200, 300]]\n",
    "    spirals = [spiral_gc(length=length) for length in [0, 100, 200]]\n",
    "    rings = []\n",
    "    for length_x in [10, 20, 30]:\n",
    "        ring = gf.components.ring_single_heater(length_x=length_x)\n",
    "        c = gf.components.add_fiber_array_optical_south_electrical_north(\n",
    "            component=ring,\n",
    "            electrical_port_names=[\"l_e2\", \"r_e2\"],\n",
    "            grating_coupler=gf.components.grating_coupler_te, \n",
    "            pad=gf.components.pad,\n",
    "            cross_section_metal='metal3'\n",
    "        ).copy()\n",
    "        c.name = f\"ring_{length_x}\"\n",
    "        c.info[\"doe\"] = \"ring_length_x\"\n",
    "        c.info[\"measurement\"] = \"optical_spectrum\"\n",
    "        c.info[\"ports_electrical\"] = 2\n",
    "        c.info[\"ports_optical\"] = 4\n",
    "        c.info[\"analysis\"] = \"[fsr]\"\n",
    "        c.info[\"analysis_parameters\"] = \"[]\"\n",
    "        label_farthest_right_port(\n",
    "            c,\n",
    "            c.ports.filter(port_type=\"vertical_te\"),\n",
    "            layer=layer_label,\n",
    "            text=f\"opt-{c.info['ports_optical']}-{c.name}\",\n",
    "        )\n",
    "        label_farthest_right_port(\n",
    "            c,\n",
    "            c.ports.filter(port_type=\"electrical\"),\n",
    "            layer=layer_label,\n",
    "            text=f\"elec-{c.info['ports_electrical']}-{c.name}\",\n",
    "        )\n",
    "        rings.append(c)\n",
    "\n",
    "    copies = 3  # Number of copies of each component.\n",
    "    components = mzis * copies + rings * copies + spirals * copies\n",
    "\n",
    "    # gf.grid(components): This function is called to arrange the components in a simple, evenly spaced grid.\n",
    "    if grid:\n",
    "        return gf.grid(components)\n",
    "    \n",
    "    # gf.pack(components): This is the default path. The pack function uses a more advanced algorithm to arrange the components in a space-efficient way,\n",
    "    # which is important for minimizing the cost of fabrication. This may result in a list of one or more packed groups.\n",
    "    c = gf.pack(components)\n",
    "    \n",
    "    # The pack function can sometimes return multiple packed groups. This code checks if that is the case (len(c) > 1) and, if so,\n",
    "    # runs pack again on those groups to combine them into one final, single component. The [0] then selects that final component.\n",
    "    if len(c) > 1:\n",
    "        c = gf.pack(c)[0]\n",
    "    return c[0]\n",
    "\n",
    "\n",
    "c = sample_reticle_with_labels()\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30",
   "metadata": {},
   "source": [
    "You can also extract all test points from a GDS file using gf.labels.write_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "gdspath = c.write_gds()\n",
    "csvpath = gf.labels.write_labels(gdspath, layer_label=layer_label)\n",
    "df = pd.read_csv(csvpath)\n",
    "df = df.sort_values(by=[\"text\"])\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "custom_cell_magics": "kql"
  },
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
